package com.jxs.tourist.controller;

import com.jxs.tourist.util.FileUpload;
import com.jxs.tourist.util.JsonResult;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

@Controller
@RequestMapping("/uploadFile")
public class UploadController {
    public UploadController() {
        System.out.println("UploadController");
    }

    /**
     * 上传图片
     * @param file
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value ="/uploadImg")
    public JsonResult<String> uploadImg(@RequestParam("file") MultipartFile file, HttpServletRequest request){
        String tempPath= null;
        try {
            tempPath = FileUpload.uploadFile(file, request);
        } catch (IOException e) {
            e.printStackTrace();
        }
        if(tempPath!=null){
            return new JsonResult<String>(tempPath,"上传成功",0);
        }
        return new JsonResult<String>("","上传失败",1);
    }
}
